import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter8-EventNotification/event-mechanism/HitTestBlocker.dart';
import 'package:flutter_demo/chapter8-EventNotification/gesture-conflict/CustomTapGestureRecongnizer.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Animation Widget Demo"),
      ),
      body: ImageAnimatedWidget(),
    ),
  ));
}

class AnimatedImage extends AnimatedWidget {
  const AnimatedImage({
    Key? key,
    required Animation<double> animation,
  }) : super(key: key, listenable: animation);

  @override
  Widget build(BuildContext context) {
    final animation = listenable  as Animation<double>;
    return Center(
      child: Image.asset(
        "assets/images/avatar.png",
        width: animation.value,
        height: animation.value,
      ),
    );
  }
}

class ImageAnimatedWidget extends StatefulWidget {
  const ImageAnimatedWidget({Key? key}) : super(key: key);

  @override
  _ImageAnimatedWidgetState createState() => _ImageAnimatedWidgetState();
}

class _ImageAnimatedWidgetState extends State<ImageAnimatedWidget>
    with SingleTickerProviderStateMixin {
  late Animation<double> animation;
  late AnimationController controller;

  @override
  Widget build(BuildContext context) {
    return AnimatedImage(animation: animation);
  }

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
      duration: const Duration(seconds: 3),
      vsync: this,
    );

    //  匀速
    // 图片宽高从0变到300
    animation = Tween(begin: 0.0, end: 300.0).animate(controller);
    //  启动动画(正向执行)
    controller.forward();
  }

  @override
  void dispose() {
    //  路由销毁释放动画资源
    controller.dispose();
    super.dispose();
  }
}
